package com.bytesizedhosting.api.android.darthcrap.screens.maintabs;

import java.text.SimpleDateFormat;
import java.util.Arrays;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.bytesizedhosting.api.android.darthcrap.Constants;
import com.bytesizedhosting.api.android.darthcrap.ExtendedActivity;
import com.bytesizedhosting.api.android.darthcrap.R;
import com.bytesizedhosting.api.android.darthcrap.api.APIController;
import com.bytesizedhosting.api.android.darthcrap.screens.KeySetScreen;
import com.bytesizedhosting.api.android.darthcrap.screens.LoadingScreen;

public class SettingsTab extends ExtendedActivity {
	SharedPreferences objSettings;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settingstab);
        
        objSettings = this.getSharedPreferences(Constants.PREFS_FILE, Context.MODE_PRIVATE);
        
        final ExtendedActivity objActivity = this;
        
        ((TextView)findViewById(R.id.lblLastUpdate)).setText(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(APIController.getData().getLastModifiedTime()));
        findViewById(R.id.btnUpdate).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View objView) {
				objActivity.switchTo(LoadingScreen.class, true, false, new IntentConfigurator() {
					@Override
					public void configure(Intent objIntent) {
						objIntent.putExtra("code", 4);
					}
				});
			}
		});
        
        updateAPIKey();
        findViewById(R.id.btnChangeAPIKey).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View objView) {
				objActivity.switchTo(KeySetScreen.class, false, 1);
			}
		});
        
        Spinner objSpinner = ((Spinner)findViewById(R.id.spnWidgetUpdateInterval));
        ArrayAdapter<CharSequence> objAdapter = ArrayAdapter.createFromResource(this, R.array.settings_spinner_widgetupdateinterval_text, android.R.layout.simple_spinner_item);
        objAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        objSpinner.setAdapter(objAdapter);
        
        int[] arrValues = this.getResources().getIntArray(R.array.settings_spinner_widgetupdateinterval_values);
        int intIndex = 0;
        int intFoundIndex = 0;
        int intCurrentValue = objSettings.getInt(Constants.PREFS_KEY_UPDATEFREQ,-1); 
        if ((intFoundIndex = Arrays.binarySearch(arrValues, intCurrentValue)) >= 0) {
        	intIndex = intFoundIndex;
        }
        
        objSpinner.setSelection(intIndex);
        
        objSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> objParent, View objView, int intPosition, long intID) {
					int intValue = objActivity.getResources().getIntArray(R.array.settings_spinner_widgetupdateinterval_values)[intPosition];
					SharedPreferences.Editor objEditor = getSharedPreferences(Constants.PREFS_FILE, Context.MODE_PRIVATE).edit();
					objEditor.putInt(Constants.PREFS_KEY_UPDATEFREQ, intValue);
					objEditor.commit();
					((TextView)findViewById(R.id.lblWidgetUpdateInterval)).setText(objParent.getItemAtPosition(intPosition).toString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// Do Nothing
			}
        });
       	((TextView)findViewById(R.id.lblWidgetUpdateInterval)).setText(objSpinner.getSelectedItem().toString());
    }
    
    private void updateAPIKey() {
		String strAPIKey = objSettings.getString(Constants.PREFS_KEY_APIKEY, "");
		if (strAPIKey.matches("[0-9a-f]{40}")) {
			((TextView)findViewById(R.id.lblAPIKey)).setText(Html.fromHtml(strAPIKey));
		}
		else {
			((TextView)findViewById(R.id.lblAPIKey)).setText(Html.fromHtml("<i>" + getString(R.string.settings_cannotdisplayapi) + "</i>"));
		}
    }
    
    public void onActivityResult(int intRequestCode, int intResultCode, Intent objIntent) { 
    	if (intRequestCode == 1 && intResultCode != RESULT_CANCELED) {
    		updateAPIKey();
    	}
   	}
}